using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;

namespace UserManagement.Models
{
    public class User : INotifyPropertyChanged
    {
        private int _id;
        private string _name = string.Empty;
        private string _email = string.Empty;
        private int _age;
        private DateTime _createDate = DateTime.Now;
        private bool _isActive = true;

        public int Id
        {
            get { return _id; }
            set { SetProperty(ref _id, value); }
        }

        [Required(ErrorMessage = "姓名不能为空")]
        [StringLength(50, ErrorMessage = "姓名长度不能超过50个字符")]
        public string Name
        {
            get { return _name; }
            set { SetProperty(ref _name, value); }
        }

        [Required(ErrorMessage = "邮箱不能为空")]
        [EmailAddress(ErrorMessage = "邮箱格式不正确")]
        public string Email
        {
            get { return _email; }
            set { SetProperty(ref _email, value); }
        }

        [Range(18, 100, ErrorMessage = "年龄必须在18-100之间")]
        public int Age
        {
            get { return _age; }
            set { SetProperty(ref _age, value); }
        }

        public DateTime CreateDate
        {
            get { return _createDate; }
            set { SetProperty(ref _createDate, value); }
        }

        public bool IsActive
        {
            get { return _isActive; }
            set { SetProperty(ref _isActive, value); }
        }

        public event PropertyChangedEventHandler? PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }
    }
}